package org.lanqiao.tjut.enity;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lanqiao.tjut.db.DBDriverFactory;
import org.lanqiao.tjut.edu.EduServer;

public class Student {

	/**
	 * 学生功能菜单
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void logStudent(int inputStuid) {
		EduServer edu = new EduServer();
		Scanner sc = new Scanner(System.in);
		System.out.println("***************************");
		System.out.println("*      1.查询个人信息");
		System.out.println("*      2.修改密码");
		System.out.println("*      3.查询同学信息");
		System.out.println("*      4.添加好友");
		System.out.println("*      5.显示我的所有好友信息");
		System.out.println("*      6.修改好友备注");
		System.out.println("*      7.删除好友");
		System.out.println("*      0.返回");
		System.out.println("***************************");
		System.out.println("请选择功能菜单：");
		int funNum = sc.nextInt();
		switch (funNum) {
		case 1:
			// 查询个人信息
			selectInfo(inputStuid);
			break;
		case 2:
			// 修改密码
			updatePWord(inputStuid);
			break;
		case 3:
			// 查询同学信息
			queryStuMenu(inputStuid);
			break;
		case 4:
			// 添加好友
			addFriends(inputStuid);
			break;
		case 5:
			// 显示所有好友信息
			allFriends(inputStuid);
			break;
		case 6:
			// 修改好友备注
			updeRename(inputStuid);
			break;
		case 7:
			// 删除好友
			deleFriends(inputStuid);
			break;
		default:
			edu.Login();
			break;
		}
	}

	/**
	 * 1.查询个人信息
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void selectInfo(int inputStuid) {
		System.out.println("***查询个人信息***");
		String strSql = "SELECT STUID,STUNAME,STUID,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' AND STUID = ?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
				new Object[] { inputStuid });
		Print pl = new Print();
		pl.printList(lst);
		System.out.println("-------------------------------------------------------------");
		logStudent(inputStuid);
	}

	/**
	 * 2.修改密码
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void updatePWord(int inputStuid) {
		System.out.println("***修改密码***");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入您的旧密码：");
		String oldPassWord = sc.next();
		String strSql = "SELECT STUPSW FROM EDUSTUDENT WHERE STUID=?";
		String passwordNum = (String) DBDriverFactory.getDBDriverInstance().query(strSql, new ScalarHandler(),
				new Object[] { inputStuid });
		if (oldPassWord.equals(passwordNum)) {
			System.out.println("请输入您的新密码：");
			String newPassWord1 = sc.next();
			System.out.println("请再次输入您的新密码：");
			String newPassWord2 = sc.next();
			if (newPassWord1.equals(newPassWord2)) {
				String strSql1 = "UPDATE EDUSTUDENT SET STUPSW='" + newPassWord1 + "' WHERE STUID=?";
				int i = DBDriverFactory.getDBDriverInstance().update(strSql1, new Object[] { inputStuid });
				if (i > 0) {
					System.out.println("密码修改成功！");
				} else {
					System.out.println("密码修改失败！");
				}
			} else {
				System.out.println("您两次输入新密码不一致，请重新输入！");
				updatePWord(inputStuid);
			}
		} else {
			System.out.println("您输入的旧密码不正确！请重新输入");
			updatePWord(inputStuid);
		}
		System.out.println("-------------------------------------------------------------");
		logStudent(inputStuid);
	}

	/**
	 * 3.学生查询功能菜单
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void queryStuMenu(int inputStuid) {
		System.out.println("***查询同学信息***");
		Scanner sc = new Scanner(System.in);
		System.out.println("1.查询所有学生信息");
		System.out.println("2.根据学号查询单个学生信息");
		System.out.println("3.根据姓名模糊查询学生信息");
		System.out.println("4.根据地址模糊查询学生信息");
		System.out.println("5.根据年龄段查询学生信息");
		System.out.println("0.返回");
		int inIput = sc.nextInt();
		switch (inIput) {
		case 1:
			// 查询所有学生信息
			selectAllStu(inputStuid);
			break;
		case 2:
			// 根据指定编号(学号)查询单个学生信息
			selectOneStu(inputStuid);
			break;
		case 3:
			// 根据姓名模糊查询学生信息
			selectNameStu(inputStuid);
			break;
		case 4:
			// 根据地址模糊查询学生信息
			selectAdreStu(inputStuid);
			break;
		case 5:
			// 根据年龄段查询学生信息
			selectAgeStu(inputStuid);
			break;
		case 0:
			logStudent(inputStuid);
			break;
		default:
			System.out.println("输入错误，请重新输入！");
			queryStuMenu(inputStuid);
		}
	}

	/**
	 * 4.添加好友
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void addFriends(int inputStuid) {
		Scanner sc = new Scanner(System.in);
		System.out.println("你是否是第一次添加好友？(1-是 * 0-不是)");
		int j = sc.nextInt();
		if (j == 1) {
			String strSql1 = "SELECT  STUID,STUNAME FROM EDUSTUDENT WHERE  STUID!=?";
			addFriends1(inputStuid, strSql1);
		} else {
			String strSql2 = "SELECT DISTINCT E.STUID,E.STUNAME FROM EDUSTUDENT E,EDUCLASSMATE F WHERE (E.STUID!=F.FRIENDID AND E.STUID!=?)";
			addFriends1(inputStuid, strSql2);
		}
	}

	/**
	 * 5.我的所有好友信息
	 * 
	 * @param inputStuid
	 */
	public void allFriends(int inputStuid) {
		Scanner sc = new Scanner(System.in);
		Print pl = new Print();
		System.out.println("您的好友信息如下：");
		String strSql4 = "SELECT FRIENDID,FRIENDNAME,BEIZHU FROM EDUCLASSMATE WHERE STUID=?";
		List lst2 = DBDriverFactory.getDBDriverInstance().query(strSql4, new MapListHandler(),
				new Object[] { inputStuid });
		pl.printList(lst2);
		System.out.println("0-返回");
		int in = sc.nextInt();
		if(in==0){
			logStudent(inputStuid);
		}
	}

	/**
	 * 6.删除好友
	 * 
	 * @param inputStuid
	 */
	public void deleFriends(int inputStuid) {
		allFriss(inputStuid);
		Scanner sc = new Scanner(System.in);
		System.out.println("输入要删除的好友学号：");
		int inpuNum = sc.nextInt();
		String strSql = "DELETE FROM EDUCLASSMATE WHERE FRIENDID=?";
		int i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { inpuNum });
		if (i > 0) {
			System.out.println("该好友删除成功！");
		} else {
			System.out.println("该好友删除失败！");
		}
		allFriends(inputStuid);
	}

	/**
	 * 7.修改好友备注
	 * 
	 * @param inputStuid
	 */
	public void updeRename(int inputStuid) {
		allFriss(inputStuid);
		Scanner sc = new Scanner(System.in);
		System.out.println("输入要修改备注的好友学号：");
		int inpuNum = sc.nextInt();
		System.out.println("请输入TA的新备注：");
		String newReName = sc.next();
		String strSql = "UPDATE EDUCLASSMATE SET BEIZHU = '" + newReName + "' WHERE FRIENDID = ?";
		int i = DBDriverFactory.getDBDriverInstance().update(strSql, new Object[] { inpuNum });
		if (i > 0) {
			System.out.println("备注修改成功！");
		} else {
			System.out.println("备注修改失败！");
		}
		allFriends(inputStuid);
	}

	/**
	 * 第一次添加好友
	 * 
	 * @param inputStuid
	 */
	public void addFriends1(int inputStuid, String strSql1) {
		Scanner sc = new Scanner(System.in);
		System.out.println("以下同学不是您的好友：");
		List lst1 = DBDriverFactory.getDBDriverInstance().query(strSql1, new MapListHandler(),
				new Object[] { inputStuid });
		Print pl = new Print();
		pl.printList(lst1);
		System.out.println("想加TA为好友，输入TA的学号：");
		int inputID = sc.nextInt();
		String strSql2 = "SELECT STUNAME FROM EDUSTUDENT WHERE STUID =?";
		String myName = (String) DBDriverFactory.getDBDriverInstance().query(strSql2, new ScalarHandler(),
				new Object[] { inputID });
		System.out.println("请给好友加个备注：");
		String inputReName = sc.next();
		String strSql3 = "INSERT INTO EDUCLASSMATE VALUES (?,?,?,?)";
		int i = DBDriverFactory.getDBDriverInstance().update(strSql3,
				new Object[] { inputStuid, inputID, myName, inputReName });
		if (i > 0) {
			System.out.println("添加好友成功！");
		} else {
			System.out.println("添加好友失败！");
		}
		allFriends(inputStuid);
		System.out.println("是否继续添加好友？(1-继续*0-返回)");
		int in = sc.nextInt();
		if(in==0){
			logStudent(inputStuid);
		} else {
			addFriends(inputStuid);
		}
	}

	/**
	 * 学生登录验证
	 */

	public void entryStudent() {
		//EduServer es = new EduServer();
		Scanner input = new Scanner(System.in);
		System.out.println("请输入账号(学号)：");
		int inputStuid = input.nextInt();
		System.out.println("请输入密码：");
		String inputPassWord = input.next();
		String strSql = "SELECT STUPSW FROM EDUSTUDENT WHERE ISDELETED = 'N' AND STUID=?";
		String passwordNum = (String) DBDriverFactory.getDBDriverInstance().query(strSql, new ScalarHandler(),
				new Object[] { inputStuid });
		if (inputPassWord.equals(passwordNum)) {
			logStudent(inputStuid);
		} else {
			System.out.println("账号或密码错误!请重新输入！");
			entryStudent();
		}
	}

	/**
	 * 1.查询所有学生信息
	 */
	public void selectAllStu(int inputStuid) {
		Scanner sc = new Scanner(System.in);
		String strSql = "SELECT STUID,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		Print pl = new Print();
		pl.printList(lst);
		// 返回上层菜单
		System.out.println("0-返回");
		int in = sc.nextInt();
		if(in==0){
			queryStuMenu(inputStuid);
		}
	}

	/**
	 * 2.根据学号查询一个同学信息
	 */
	public void selectOneStu(int inputStuid) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入您想要查询的同学的学号：");
		int inputId = sc.nextInt();
		String strSql = "SELECT STUID,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE ISDELETED = 'N' AND STUID=?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(), new Object[] { inputId });
		Print pl = new Print();
		pl.printList(lst);
		System.out.println("是否继续其他学生信息？(1-继续*0-返回)");
		int in = sc.nextInt();
		if(in==0){
			queryStuMenu(inputStuid);
		} else {
			selectOneStu(inputStuid);
		}

	}

	/**
	 * 3.根据姓名模糊查询学生信息
	 */
	public void selectNameStu(int inputStuid) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入需要姓名模糊查询的字：");
		String stName = sc.next();
		stName = "%" + stName + "%";
		String strSql = "SELECT STUID,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE STUNAME LIKE '"
				+ stName + "'";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		Print pl = new Print();
		pl.printList(lst);
		System.out.println("是否继续根据姓名模糊查询学生信息？(1-继续*0-返回)");
		int in = sc.nextInt();
		if(in==0){
			queryStuMenu(inputStuid);
		} else {
			selectNameStu(inputStuid);
		}
	}

	/**
	 * 4.根据地址模糊查询学生信息
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void selectAdreStu(int inputStuid) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入需要根据地址模糊查询的字：");
		String stAdress = sc.next();
		stAdress = "%" + stAdress + "%";
		String strSql = "SELECT STUID,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE STUADDRESS LIKE '"
				+ stAdress + "'";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler());
		Print pl = new Print();
		pl.printList(lst);
		System.out.println("是否继续根据地址模糊查询学生信息？(1-继续*0-返回)");
		int in = sc.nextInt();
		if(in==0){
			queryStuMenu(inputStuid);
		} else {
			selectAdreStu(inputStuid);
		}
	}

	/**
	 * 5.根据年龄段模糊查询学生信息
	 * 
	 * @param inputStuid
	 *            学号
	 */
	public void selectAgeStu(int inputStuid) {
		System.out.println("根据年龄段模糊查询学生信息");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入较小的年龄：");
		int stAge1 = sc.nextInt();
		System.out.println("请输入较大的年龄：");
		int stAge2 = sc.nextInt();
		String strSql = "SELECT STUID,STUNAME,STUSEX,STUAGE,STUPHONE,STUADDRESS FROM EDUSTUDENT WHERE STUAGE >=? AND STUAGE<=?";
		List lst = DBDriverFactory.getDBDriverInstance().query(strSql, new MapListHandler(),
				new Object[] { stAge1, stAge2 });
		Print pl = new Print();
		pl.printList(lst);
		System.out.println("是否继续根据年龄段模糊查询学生信息?(1-继续*0-返回)");
		int in = sc.nextInt();
		if(in==0){
			queryStuMenu(inputStuid);
		} else {
			selectAgeStu(inputStuid);
		}
	}

	/**
	 * 所有好友信息
	 * 
	 * @param inputStuid
	 */
	public void allFriss(int inputStuid) {
		Scanner sc = new Scanner(System.in);
		Print pl = new Print();
		System.out.println("您的好友信息如下：");
		String strSql4 = "SELECT FRIENDID,FRIENDNAME,BEIZHU FROM EDUCLASSMATE WHERE STUID=?";
		List lst2 = DBDriverFactory.getDBDriverInstance().query(strSql4, new MapListHandler(),
				new Object[] { inputStuid });
		pl.printList(lst2);
	}
}
